/*
 * Copyright (c)  2020,  DataPipeline
 * All rights reserved.
 *
 * Id:WebSocketConfig.java   2020-08-18 16:03 wanglijun
 */
package cn.datacare.marmot.config;

import cn.datacare.marmot.web.PrincipalHandshakeHandler;
import cn.datacare.marmot.web.WebSocketInterceptor;
import cn.datacare.marmot.web.WebsocketHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.session.Session;
import org.springframework.session.web.socket.config.annotation.AbstractSessionWebSocketMessageBrokerConfigurer;
import org.springframework.web.socket.config.annotation.*;
import org.springframework.web.socket.handler.TextWebSocketHandler;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Copyright: Copyright (c) 2020
 * </p>
 * <p>
 * Company/Department: DataPipeline
 * </p>
 *
 * @author wanglijun
 * @version 1.0
 * @since 2020-08-18 16:03
 */
@Configuration
//注解开启使用STOMP协议来传输基于代理(message broker)的消息,这时控制器支持使用@MessageMapping,就像使用@RequestMapping一样
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractSessionWebSocketMessageBrokerConfigurer<Session> implements WebSocketMessageBrokerConfigurer {

    @Autowired
    private WebSocketInterceptor webSocketInterceptor;
    @Autowired
    private PrincipalHandshakeHandler principalHandshakeHandler;



    @Override
    protected void configureStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/ws_endpoint")
                .addInterceptors(webSocketInterceptor)
                .setHandshakeHandler(principalHandshakeHandler)
                .setAllowedOrigins("*").withSockJS();
    }

    /***
     * 配置消息代理(Message Broker)
     * @param registry
     */
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/topic","/user");
        registry.setUserDestinationPrefix("/user");
    }

    @Bean
    public TextWebSocketHandler webSocketHandler(){
        return new WebsocketHandler();
    }



}
